package com.freemyleft.left.left_app.mian.index.work.completecoures;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.freemyleft.left.R2;
import com.freemyleft.left.left_app.R;
import com.freemyleft.left.left_app.mian.index.work.waitingforclass.HomeworkDelegate;
import com.freemyleft.left.left_app.mian.index.work.waitingforclass.adapter.SetGridHeight;
import com.freemyleft.left.zapp.delegates.LeftDelegate;
import com.freemyleft.left.zapp.selectimage.PicassoUtils;
import com.freemyleft.left.zapp.selectimage.activity.ImageShowActivity;
import com.freemyleft.left.zapp.selectimage.imageload.ImageLoad;

import java.io.Serializable;
import java.util.ArrayList;

import butterknife.BindView;

public class CourseDetailsDelegate extends LeftDelegate {
    @BindView(R2.id.m_grid)
    GridView gridView;
    ArrayList<String> mImageList=new ArrayList();
    private int mMax=6;
    private Adapter mAdapter = null;


    @Override
    public Object setLayout() {
        return R.layout.delegate_work_coursedetaile;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        mAdapter = new Adapter();
        gridView.setAdapter(mAdapter);
        SetGridHeight.setGvHeight(gridView, mAdapter, 5);

    }

    public class Adapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mImageList.size() == mMax ? mImageList.size() : mImageList.size() + 1;

        }

        @Override
        public Object getItem(int position) {
            return mImageList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            convertView = LayoutInflater.from(getActivity()).
                    inflate(R.layout.item_orender_image, null);
            ImageView imageView = convertView.findViewById(R.id.m_image);
            TextView textView = convertView.findViewById(R.id.tv);
            textView.setVisibility(View.GONE);
            if (mImageList.size() != position) {
                if (mImageList.get(position) != null) {//网络图片
                    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    ImageLoad.loadImgDefault(getActivity(), imageView, mImageList.get(position));
                } else {//本地图片
                    PicassoUtils.picasso(getActivity(), mImageList.get(position), 300, 300, imageView);
                    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                }
            } else {
                textView.setVisibility(View.GONE);
                imageView.setImageResource(R.mipmap.orender_jiahao);
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            }
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(_mActivity, "跳转到添加学生页面", Toast.LENGTH_SHORT).show();
                    if (mImageList.size() == position) {
                        if (mMax - mImageList.size() != 0) {
                             //更新集合 传入新的头像
                        }
                    } else {
                    }
                }
            });
            return convertView;

        }

    }
}
